package com.phpxiu.app.view.popupwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.PopupWindow;

import com.phpxiu.app.adapter.LoveStateListAdapter;
import com.phpxiu.app.kkylive.KKYApp;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.LoveState;
import com.phpxiu.app.view.UserInfoEdit;

import java.lang.ref.WeakReference;

/**
 * Created by WuJinZhou on 2016/6/4.
 */
@Deprecated
public class LoveStateSelector extends PopupWindow implements View.OnClickListener,AdapterView.OnItemClickListener{
    private final static String TAG="LoveStateSelector";
    private WeakReference<UserInfoEdit>calls;
    private GridView mGridView;
    private LoveStateListAdapter mAdapter;
    private View rootView;
    static {

    }
    public LoveStateSelector(UserInfoEdit editor) {
        super(editor);
        calls=new WeakReference<UserInfoEdit>(editor);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable());
        rootView= LayoutInflater.from(editor).inflate(R.layout.user_info_love_selector,null);
        setContentView(rootView);
        rootView.findViewById(R.id.base_view).setOnClickListener(this);
        init(editor);
    }

    /**
     * 初始化窗口
     */
    private void init(Context context){
        mGridView=(GridView)rootView.findViewById(R.id.selector_grid_view);
        mGridView.setOnItemClickListener(this);
        mAdapter=new LoveStateListAdapter(context, KKYApp.STATES);
        mGridView.setAdapter(mAdapter);
    }

    public void show(String currentId,View parent) {
        for (LoveState state: KKYApp.STATES){
            if(state.getId().equals(currentId)){
                state.setSelected(true);
            }else {
                state.setSelected(false);
            }
        }

        mAdapter.notifyDataSetChanged();
        super.showAtLocation(parent, Gravity.BOTTOM, 0,0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.base_view:
                dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(view.getTag()!=null&&view.getTag() instanceof LoveState){
            LoveState state=(LoveState)view.getTag();
            boolean isRefresh=false;
            for (LoveState s: KKYApp.STATES){
                if(state.getId().equals(s.getId())){
                    if(!s.isSelected()){
                        s.setSelected(true);
                        isRefresh=true;
                    }
                }else{
                    if(s.isSelected()){
                        s.setSelected(false);
                        isRefresh=true;
                    }
                }
            }
            if(isRefresh) {
                mAdapter.notifyDataSetChanged();
            }
            if(calls!=null&&calls.get()!=null){
                calls.get().onLoveStateItemClick(state);
            }
            dismiss();
        }
    }
}
